package org.yucc.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class AuthenrizeActivity extends Activity {
	private static final String TAG = "AuthenrizeActivity" ;
	
	private WebView web = null ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	//	requestWindowFeature(Window.FEATURE_NO_TITLE) ;
		setContentView(R.layout.authenrize) ;
		web = (WebView) findViewById(R.id.webview) ;
	
		final Intent it = getIntent() ;
		web.loadUrl(it.getDataString()) ;  
		web.getSettings().setSupportZoom(true) ;
		web.getSettings().setJavaScriptEnabled(true) ;
		web.getSettings().setBuiltInZoomControls(true) ;
		web.setWebChromeClient(new WebChromeClient(){

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				// TODO Auto-generated method stub
				AuthenrizeActivity.this.setProgress(newProgress) ;
				AuthenrizeActivity.this.setTitle(R.string.loading) ;
				if(newProgress == 100) {
					AuthenrizeActivity.this.setTitle(R.string.app_name) ;
				}
			}
		}) ;
		
		web.setWebViewClient(new WebViewClient(){
			private int index = 0 ;
			
			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
                super.onPageFinished(view, url);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				Log.v(TAG, "shouldOverrideUrlLoading webView.loadUrl") ;
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
				if(url.contains("weibo4android://OAuthActivity")&&index == 0){
					index ++ ;
					Log.v("url", url) ;
					Intent it = new Intent(AuthenrizeActivity.this,OAuthActivity.class) ;
					it.setData(Uri.parse(url)) ;
					AuthenrizeActivity.this.startActivity(it) ;
					finishActivity(1);
					setResult(RESULT_OK,new Intent(AuthenrizeActivity.this,LoginActivity.class));
					AuthenrizeActivity.this.finish();
				}
			}
			
		}) ;
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.e(TAG, "onDestroy");
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		Log.e(TAG, "onPause");
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.e(TAG, "onRestart");
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		Log.e(TAG, "onResume");
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		Log.e(TAG, "onStart");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		Log.e(TAG, "onStop");
	}
	
	
}
